<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use Validator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Hash;
use App\User;


/**
* 用户注册
*/
class RegisterController extends Controller
{
    /**
     * 显示注册页面
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //解析登录模板
        return view('home/register/index');
    }

    /**
     * 接收并处理邮箱方式注册
     *
     * @return \Illuminate\Http\Response
     */
    public function postEmailinsert(Request $request)
    {
         //接收数据
        $input = $request -> all('_token');
        
        //设置过滤规则
        $rules = [
            'email' => 'email|required',
            'password' => 'required',
            'repassword' => 'required|same:password',
        ];
        
        //设置错误消息
        $messages = [
                 'username.required' => '用户名是必填的',
                'password.required'  => '密码是必填的',
                'repassword.required'  => '检查密码是必填的',
                'repassword.same'  => '密码不一致',
                'email.email'  => '邮箱格式不正确',
        ];

        //验证数据
        $validator = Validator::make($input,$rules,$messages);
       
        if ($validator->fails()) {
            return back()
                        ->withErrors($validator)
                        ->withInput();
        }

        //格式化用户信息
        $data = $request -> only('email','password');
        $data['ctime'] = time();
        $data['token'] = str_random(50);
        $data['password'] = Hash::make($data['password']);
        
        //插入数据库
        $id = DB::table('user') -> insertGetId($data);

        //发送激活邮件
        Mail::send('/home/register/email', ['id' => $id,'token' => $data['token']], function ($m) use ($data) {
            $m->from('18230373213@163.com', 'app');

            $m->to($data['email'])->subject('这是一封激活邮件');
        });
    }

    /**
     * 用户激活
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function getActivate(Request $request)
    {
        //接收激活信息
        $data = $request -> all();
        
        //查询数据库
        $token = DB::table('user') -> where('id',$data['id']) -> value('token');
        
        //核对口令
        $res = Hash::check($token,$data['token']);
        
        //返回结果
        if ($res) {
            DB::table('user') -> update(['status' => 1]);
        }else{
            
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 处理结果的静态私有方法
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
   static private function res($res,$message,$url_su,$url_er)
    {
        //返回结果
        if ($res) {
         
           //跳转到用户列表并显示成功信息
           return redirect('admin/user/index')
                        ->with(['message'=>'添加成功','type'=>1]);
        }else{

           //跳转到添加页面
           return redirect('admin/user/add')
                        ->with(['message'=>'添加失败','type'=>2]);
        }
    }
}
